package com.lk.lkappearance.utills;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;

public class ResultEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String CODE = "code";
    private static final String MSG = "msg";
    private static final String DATA = "data";

    private int code;
    private String msg = "";
    private JSONObject data;


    public int getCode() {
        return code;
    }


    public ResultEntity setCode(int code) {
        this.code = code;
        return this;
    }



    public String getMsg() {
        return msg;
    }


    public void setData(JSONObject data) {
        this.data = data;
    }


    public JSONObject getData() {
        return data;
    }


    public ResultEntity setMsg(String msg) {
        this.msg = msg;
        return this;
    }


    public ResultEntity setSuccess() {
        code = ResultCode.SUCCESS.CODE;
        msg = ResultCode.SUCCESS.MESSAGE;
        return this;
    }


    public ResultEntity setSuccess(String message) {
        code = ResultCode.SUCCESS.CODE;
        msg = message;
        return this;
    }

    public ResultEntity setFail() {
        code = ResultCode.FAIL.CODE;
        msg = ResultCode.FAIL.MESSAGE;
        return this;
    }


    public ResultEntity setNotRegister() {
        code = ResultCode.NOT_REGISTER.CODE;
        msg = ResultCode.NOT_REGISTER.MESSAGE;
        return this;
    }

    public static JSONObject setJsonTokenOutTime() {
        JSONObject result = new JSONObject();
        result.put(CODE, ResultCode.TOKEN_OUT_TIME.CODE);
        result.put(MSG, ResultCode.TOKEN_OUT_TIME.MESSAGE);
        return result;
    }

    public static JSONObject setJsonSqlSecurity() {
        JSONObject result = new JSONObject();
        result.put(CODE, ResultCode.SQL_SECURITY.CODE);
        result.put(MSG, ResultCode.SQL_SECURITY.MESSAGE);
        return result;
    }

    public static JSONObject setJsonNoPermissions() {
        JSONObject result = new JSONObject();
        result.put(CODE, ResultCode.NO_PERMISSIONS.CODE);
        result.put(MSG, ResultCode.NO_PERMISSIONS.MESSAGE);
        return result;
    }

    public static JSONObject setUserDISABLED() {
        JSONObject result = new JSONObject();
        result.put(CODE, ResultCode.USER_DISABLED.CODE);
        result.put(MSG, ResultCode.USER_DISABLED.MESSAGE);
        return result;
    }

    public static JSONObject setUserQuit() {
        JSONObject result = new JSONObject();
        result.put(CODE, ResultCode.USER_QUIT.CODE);
        result.put(MSG, ResultCode.USER_QUIT.MESSAGE);
        return result;
    }

    public static JSONObject setDateDel() {
        JSONObject result = new JSONObject();
        result.put(CODE, ResultCode.DATA_DEL.CODE);
        result.put(MSG, ResultCode.DATA_DEL.MESSAGE);
        return result;
    }

    public static JSONObject setJsonTokenSuccess() {
        JSONObject result = new JSONObject();
        result.put(CODE, ResultCode.SUCCESS.CODE);
        result.put(MSG, ResultCode.SUCCESS.MESSAGE);
        return result;
    }

    public ResultEntity setFail(String message) {
        code = ResultCode.FAIL.CODE;
        msg = message;
        return this;
    }

    public ResultEntity setTokenOutTime() {
        code = ResultCode.TOKEN_OUT_TIME.CODE;
        msg = ResultCode.TOKEN_OUT_TIME.MESSAGE;
        return this;
    }

    public ResultEntity getTokenOutTime() {
        this.code = ResultCode.TOKEN_OUT_TIME.CODE;
        this.msg = ResultCode.TOKEN_OUT_TIME.MESSAGE;
        return this;
    }


    public ResultEntity setNoPermissions() {
        code = ResultCode.NO_PERMISSIONS.CODE;
        msg = ResultCode.NO_PERMISSIONS.MESSAGE;
        return this;
    }

    public ResultEntity setError() {
        code = ResultCode.ERROR.CODE;
        msg = ResultCode.ERROR.MESSAGE;
        return this;
    }

    public ResultEntity setRegistered() {
        code = ResultCode.REGISTERED.CODE;
        msg = ResultCode.REGISTERED.MESSAGE;
        return this;
    }

    public ResultEntity setResult(int result) {
        if (result <= 0) {
            code = ResultCode.FAIL.CODE;
            msg = ResultCode.FAIL.MESSAGE;
        } else {
            code = ResultCode.SUCCESS.CODE;
            msg = ResultCode.SUCCESS.MESSAGE;
        }
        return this;
    }


    public ResultEntity addData(String key, Object value) {
        if (data == null) {
            data = new JSONObject();
        }
        data.put(key, value);
        return this;
    }

    public <T> ResultEntity setData(T t) {
        data = JSON.parseObject(JSON.toJSONString(t, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty));
        return this;
    }

    public JSONObject getResult() {
        JSONObject result = new JSONObject();
        result.put(CODE, code);
        result.put(MSG, msg);
        if (data != null) {
            result.put(DATA, data);
        }
        return result;
    }

}
